Passed
Pull Request — master (#87)
by Mathieu
01:37
created

UploadFileCommandHandler   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 17
dl 0
loc 18
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 9 1
1
import {CommandHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {UploadFileCommand} from './UploadFileCommand';
4
import {IFileStorage} from 'src/Application/IFileStorage';
5
import {IFileRepository} from 'src/Domain/File/Repository/IFileRepository';
6
import {File} from 'src/Domain/File/File.entity';
7
8
@CommandHandler(UploadFileCommand)
9
export class UploadFileCommandHandler {
10
  constructor(
11
    @Inject('IFileStorage')
12
    public readonly fileStorage: IFileStorage,
13
    @Inject('IFileRepository')
14
    public readonly fileRepository: IFileRepository
15
  ) {}
16
17
  public async execute(command: UploadFileCommand): Promise<string> {
18
    const {uploadedFile} = command;
19
    const fileName = await this.fileStorage.upload(uploadedFile);
20
    const file = await this.fileRepository.save(
21
      new File(fileName, uploadedFile.size, uploadedFile.mimetype)
22
    );
23
24
    return file.getId();
25
  }
26
}
27